package me.yuzho.study.concurrency;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/** 后台线程创建的线程缺省就是后台线程
 * @author yzhou-citrix
 *
 */
public class Daemon implements Runnable {
    
    private Thread[] t = new Thread[10];

    @Override
    public void run() {
        
        for(int i=0; i < t.length; i++) {
            t[i] = new Thread(new DaemonSpawn());
            t[i].start();
            
            System.out.println(String.format("DaemonSpawn %d started", i));
        } 
        
        IntStream.range(0, t.length).forEach(i -> {
            System.out.println(String.format("t[%d].isDaemon() = %s", i, t[i].isDaemon()));
        });
        
        while(true) {
            Thread.yield();
        }

    }
    
    class DaemonSpawn implements Runnable {

        @Override
        public void run() {
            while(true) {
                Thread.yield();
            }
        }
        
    }

    public static void main(String[] args) throws InterruptedException {
        
        Thread d = new Thread(new Daemon());
        
        d.setDaemon(true);
        d.start();
        
        System.out.println(String.format("d.isDaemon() = %s",  d.isDaemon()));
        
        TimeUnit.SECONDS.sleep(1);

    }

}
